home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / GWMALLOC.ZIP;1 / GWMALLOC.TAR / gw_malloc / malloc_lp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-08  |  4.0 KB  |  145 lines

  1. /*
  2.  * leap-frog point to allow malloc-debug on/off via relink.
  3.  *
  4.  * Copyright 1992 by Gray Watson and the Antaire Corporation
  5.  *
  6.  * This file is part of the malloc-debug package.
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library (see COPYING-LIB); if not, write to the
  20.  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * The author of the program may be contacted at gray.watson@antaire.com
  23.  */
  24.  
  25. /*
  26.  * If anyone can think of a better way to do this *please* let me know.
  27.  *
  28.  * The goal is to allow people to use the debug library during development
  29.  * and then disable and return to the system (or more efficient) malloc
  30.  * functions by just relinking with another library.
  31.  *
  32.  * The problem is that we want to provide the library with file/line
  33.  * information with each call.  Backtracing the stack-frame is neither
  34.  * portable or necessarily easy so the cpp __FILE__ and __LINE__ directives
  35.  * are used instead.  But how to pass this information to the library is
  36.  * the challange.  How can we do this in a graceful and sane manner while
  37.  * still providing the "quick-release" functionality above?
  38.  *
  39.  * I have decided on this leap-frog file that will have to *always* be
  40.  * compiled in.  It will have the special _malloc_file and _malloc_line
  41.  * variables and will set them whether malloc-debug routines are linked
  42.  * in to use them or not.
  43.  *
  44.  * This *will* mean an extra function call per memory interaction but on
  45.  * most systems this is pretty cheap.  It will also mean that this file
  46.  * needs to be compiled into your programs whether or not they use the
  47.  * debug library unless you want to recompile all .c files with
  48.  * -DMALLOC_DEBUG_DISABLE.
  49.  *
  50.  * Please mail me with any reasonable ideas.
  51.  */
  52.  
  53. #define MALLOC_DEBUG_DISABLE
  54.  
  55. #include "malloc.h"
  56.  
  57. #if INCLUDE_RCS_IDS
  58. static    char    *rcs_id =
  59.   "$Id: malloc_lp.c,v 1.5 1993/04/01 04:59:25 gray Exp $";
  60. #endif
  61.  
  62. /*
  63.  * exported variables
  64.  */
  65. /* to inform the malloc-debug library from which file the call comes from */
  66. EXPORT    char        *_malloc_file = MALLOC_DEFAULT_FILE;
  67.  
  68. /* to inform the library from which line-number the call comes from */
  69. EXPORT    unsigned int    _malloc_line = MALLOC_DEFAULT_LINE;
  70.  
  71. /*
  72.  * leap routine to calloc
  73.  */
  74. EXPORT    char    *_calloc_leap(const char * file, const int line,
  75.                   unsigned int elen, unsigned int size)
  76. {
  77.   char    *ret;
  78.   
  79.   _malloc_file = (char *)file;
  80.   _malloc_line = line;
  81.   
  82.   ret = calloc(elen, size);
  83.   
  84.   _malloc_file = MALLOC_DEFAULT_FILE;
  85.   _malloc_line = MALLOC_DEFAULT_LINE;
  86.   
  87.   return ret;
  88. }
  89.  
  90. /*
  91.  * leap routine to free
  92.  */
  93. EXPORT    int    _free_leap(const char * file, const int line, char * pnt)
  94. {
  95.   int    ret;
  96.   
  97.   _malloc_file = (char *)file;
  98.   _malloc_line = line;
  99.   
  100.   ret = free(pnt);
  101.   
  102.   _malloc_file = MALLOC_DEFAULT_FILE;
  103.   _malloc_line = MALLOC_DEFAULT_LINE;
  104.   
  105.   return ret;
  106. }
  107.  
  108. /*
  109.  * leap routine to malloc
  110.  */
  111. EXPORT    char    *_malloc_leap(const char * file, const int line,
  112.                   unsigned int size)
  113. {
  114.   char    *ret;
  115.   
  116.   _malloc_file = (char *)file;
  117.   _malloc_line = line;
  118.   
  119.   ret = malloc(size);
  120.   
  121.   _malloc_file = MALLOC_DEFAULT_FILE;
  122.   _malloc_line = MALLOC_DEFAULT_LINE;
  123.   
  124.   return ret;
  125. }
  126.  
  127. /*
  128.  * leap routine to realloc
  129.  */
  130. EXPORT    char    *_realloc_leap(const char * file, const int line, char * oldp,
  131.                    unsigned int new_size)
  132. {
  133.   char    *ret;
  134.   
  135.   _malloc_file = (char *)file;
  136.   _malloc_line = line;
  137.   
  138.   ret = realloc(oldp, new_size);
  139.   
  140.   _malloc_file = MALLOC_DEFAULT_FILE;
  141.   _malloc_line = MALLOC_DEFAULT_LINE;
  142.   
  143.   return ret;
  144. }
  145.